<template>
  <div class="upload-container icon-wrap">
    <el-upload
      :data="dataObj"
      :multiple="false"
      :show-file-list="false"
      :on-change="onchange"
      class="image-uploader"
      drag
      action=""
    >
      <i class="icon-plus el-icon-plus" />
      <div class="el-upload__text">
        将文件拖到此处，或<em>点击上传</em>
      </div>
    </el-upload>
    <span style="color:red">(支持格式为.jpg.png.bmp.jpeg,且小于1MB，尺寸推荐274*146)</span>

    <div v-show="tempUrl != ''" class="image-preview">
      <div class="image-preview-wrapper">
        <img :src="tempUrl">
        <div class="image-preview-action">
          <i class="el-icon-delete" @click="rmImage" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getToken } from '@/api/qiniu'
import { Message } from 'element-ui'

export default {
  name: 'SingleImageUpload',
  props: {
    value: {
      type: String,
      default: ''
    },
    flg: {
      type: String,
      default: 'create'
    },
  },
  data() {
    return {
      tempUrl: '',
      dataObj: { token: '', key: '' },
      dataType: ['image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png']
    }
  },
  computed: {
    imageUrl() {
      return this.value
    }
  },
  methods: {
    rmImage() {
      this.tempUrl = ''
      this.emitInput('')
      this.$emit('func', { img1: '' })
    },
    emitInput(val) {
      this.$emit('input', val)
    },
    handleImageSuccess() {
      this.emitInput(this.tempUrl)
    },
    beforeUpload() {
      const _self = this
      return new Promise((resolve, reject) => {
        getToken().then(response => {
          const key = response.data.qiniu_key
          const token = response.data.qiniu_token
          _self._data.dataObj.token = token
          _self._data.dataObj.key = key
          this.tempUrl = response.data.qiniu_url
          resolve(true)
        }).catch(err => {
          console.log(err)
          reject(false)
        })
      })
    },
    onchange(file,fileList) {
      var _this = this
      var event = event || window.event
      if (event.target.name == 'file') {
        var file = event.target.files[0]
        if (Math.floor(file.size / 1024 / 1024) > 1) {
          Message({
            message: '文件大于1M',
            type: 'error',
            duration: 5 * 1000
          })
          return
        } else {
          let flg = false
          this.dataType.forEach(item => {
            if (file.type == item) {
              flg = true
              return false
            }
          })
          if (!flg) {
            Message({
              message: '文件类型不匹配',
              type: 'error',
              duration: 5 * 1000
            })
            return
          } else {
            var reader = new FileReader();
            //转base64
            reader.onload = function(e) {
              _this.tempUrl = e.target.result //将图片路径赋值给src
              _this.$emit('func', { img1: _this.tempUrl })
            }
            reader.readAsDataURL(file)
          }
        }
      }
    }
  }
}
</script>

<style lang="scss">
    @import "~@/styles/mixin.scss";
    .icon-wrap {
      display: flex;
      width: 600px;
      max-width: 700px;
      min-width: 100%
    }
    .upload-container .el-upload .el-upload-dragger {
      height: 146px !important;
      width: 274px !important;
    }
    .icon-plus.el-icon-plus {
      font-size: 3em
    }
    .upload-container {
  height: 100%;
  position: relative;
  .image-uploader {
    height: 100%;
  }
  .image-preview {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0px;
    top: 0px;
    border: 1px dashed #d9d9d9;
    .image-preview-wrapper {
      position: relative;
      width: 100%;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .image-preview-action {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, .5);
      transition: opacity .3s;
      cursor: pointer;
      text-align: center;
      line-height: 200px;
      .el-icon-delete {
        font-size: 36px;
      }
    }
    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }
}

</style>
